//
//  GoodStandardView.m
//  B2C
//
//  Created by xiaowu on 2017/8/2.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "GoodStandardView.h"
#import "GoodStandardSelectCell.h"
#import "CartCountControlView.h"
#import "SKUManager.h"

static CGFloat ViewHeight = 400; // 463 + 49;

@interface GoodStandardView ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>{
    CGRect _keyboardBenginFrame;
    CGRect _keyboardEndFrame;
    NSTimeInterval _keyboardAnimationDuration;
}
// UI
@property (nonatomic, strong) UIView *mainView;

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *skuCountLabel;
@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) UIButton *addCartButton;
@property (nonatomic, strong) UIButton *buyButton;

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) CartCountControlView *controlView;

@property (nonatomic, strong) UIView *maskView;
// Data
@property (nonatomic, strong) UIColor *currentColor;

@property (nonatomic, strong) NSMutableArray <GoodAttributeModel *> *dataArr;
@property (nonatomic, strong) NSMutableArray <GoodsAttributePriceModel *> *goodsattributeArr;
@property (nonatomic, assign) GoodStandardViewType currentType;

@property (nonatomic, strong) NSMutableArray *selectArr;

@property (nonatomic, copy) NSString *showPrice;
@property (nonatomic, copy) NSString *currentPrice;
@property (nonatomic, copy) NSString *currentPGPrice;

//@property (nonatomic, copy) NSString *showSku;
//@property (nonatomic, copy) NSString *currentSku;
@property (nonatomic, strong) GoodsAttributePriceModel *responsePriceModel;
@end

@implementation GoodStandardView

+ (GoodStandardView *)showGoodStandardViewWithTitle:(NSString *)title
                                              price:(NSString *)price
                                            pgprice:(NSString *)pgprice
                                              count:(NSString *)count
                                           imageURL:(NSString *)imageUrl
                              goodAttributeModelArr:(NSArray <GoodAttributeModel *> *)attribute
                        goodsAttributePriceModelArr:(NSArray <GoodsAttributePriceModel *> *)goodsattribute
                                               type:(GoodStandardViewType)type
                                          needReset:(BOOL)need
                                              block:(void (^)(NSInteger buttonIndex, NSMutableArray <GoodAttributeValueModel *> *arr,  GoodsAttributePriceModel *priceModel, NSString *count))block
{
    GoodStandardView *standardView = [[GoodStandardView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    if (!window) {
        window = [[UIApplication sharedApplication].windows lastObject];
    }
    
    [window addSubview:standardView];
    [window bringSubviewToFront:standardView];
    [standardView bottomToTopAnimation];
    standardView.titleLabel.text = title;
    standardView.currentPrice = price;
    standardView.currentPGPrice = pgprice;
    standardView.showPrice = price;

    [standardView.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:PlaceholderImageSmall changeContentModel:UIViewContentModeScaleAspectFill];
    standardView.currentType = type;
    standardView.dataArr = [NSMutableArray arrayWithArray:attribute];
    standardView.goodsattributeArr = [NSMutableArray arrayWithArray:goodsattribute];
    standardView.controlView.textField.text = count;

    [standardView setSelectStandards];
    if (need) {
        [standardView resetStandards];
    }
    [standardView reloadUI];
    
    standardView.block = block;
    return standardView;
}

- (void)resetStandards {
    for (GoodAttributeModel *attributeModel in self.dataArr) {
        for (GoodAttributeValueModel *valueModel in attributeModel.twoattribute) {
            valueModel.isSelected = NO;
        }
    }
}

- (void)setSelectStandards {
    for (NSInteger i = 0; i < self.dataArr.count; i++) {
        GoodAttributeModel *attributeModel = [self.dataArr safeObjectAtIndex:i];
        for (NSInteger j = 0; j < attributeModel.twoattribute.count; j++) {
            GoodAttributeValueModel *valueModel = [attributeModel.twoattribute safeObjectAtIndex:j];
            if (valueModel.isSelected) {
                [self.selectArr replaceObjectAtIndex:i withObject:@(j)];
            }
        }
    }
}

- (void)dealloc {
    NSLog(@"GoodStandardView dealloc");
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        _dataArr = [NSMutableArray array];
        _goodsattributeArr = [NSMutableArray array];
        _selectArr = [NSMutableArray array];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissSelf)];
        [self.mainView addGestureRecognizer:tapGesture];
        
        [self.tableView registerClass:[GoodStandardSelectCell class] forCellReuseIdentifier:@"cell"];
        
        [self configureFooter];
        
        _currentType = GoodStandardViewTypeForAll;
        _currentColor = [UIColor orangePriceColor];
//        [self.tableView registerNib:[UINib nibWithNibName:@"ServiceInfoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
        _responsePriceModel = [[GoodsAttributePriceModel alloc] init];
        [self addObserver];
        [self responseBlock];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    [self addSubview:self.mainView];
    [self addSubview:self.backView];
    
    [self.backView addSubview:self.imageView];
    [self.backView addSubview:self.titleLabel];
    [self.backView addSubview:self.priceLabel];
    [self.backView addSubview:self.skuCountLabel];
    [self.backView addSubview:self.lineView];
    
    [self.backView addSubview:self.tableView];
    
    [self.backView addSubview:self.addCartButton];
    [self.backView addSubview:self.buyButton];
    [self addSubview:self.maskView];
}

- (CGFloat)boundingWithStr:(NSString *)str {
    return ceilf([str boundingRectWithSize:CGSizeMake(ScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:TextPingFangFontSize(14)} context:nil].size.width);
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.mainView.frame = [UIScreen mainScreen].bounds;
    self.backView.frame = CGRectMake(0, self.height - ViewHeight, self.width, ViewHeight);
    self.maskView.frame = self.backView.frame;
    
    self.imageView.frame = CGRectMake(15, 15, self.imageView.width, self.imageView.height);
    
    self.titleLabel.frame = CGRectMake(self.imageView.right + 10, self.imageView.top, self.width - self.titleLabel.origin.x - 15, self.imageView.height * 0.5);
    self.priceLabel.frame = CGRectMake(self.imageView.right + 10, self.imageView.top + self.imageView.height * 0.5, self.priceLabel.width, self.imageView.height * 0.5);
    self.skuCountLabel.frame = CGRectMake(self.priceLabel.right + 10, self.priceLabel.mj_y, self.skuCountLabel.width, self.imageView.height * 0.5);
    self.lineView.frame = CGRectMake(15, self.imageView.bottom + 15, self.width - 30, 0.5);
    
    self.tableView.frame = CGRectMake(0, self.lineView.bottom, self.width, self.backView.height - self.lineView.bottom - 49);
    
    if (self.currentType == GoodStandardViewTypeForAddCart) {
        self.addCartButton.frame = CGRectMake(0, self.tableView.bottom, self.width, 49);
    } else if (self.currentType == GoodStandardViewTypeForBuy) {
        self.buyButton.frame = CGRectMake(0, self.tableView.bottom, self.width, 49);
    } else if (self.currentType == GoodStandardViewTypeForAll) {
        self.addCartButton.frame = CGRectMake(0, self.tableView.bottom, self.width * 0.5, 49);
        self.buyButton.frame = CGRectMake(self.width * 0.5, self.tableView.bottom, self.width * 0.5, 49);
    } else if (self.currentType == GoodStandardViewTypeIntegral) {
        self.buyButton.frame = CGRectMake(0, self.tableView.bottom, self.width, 49);
        [self.buyButton setTitle:@"立即兑换" forState:UIControlStateNormal];
    } else if (self.currentType == GoodStandardViewTypeGroupAll) {
        self.addCartButton.frame = CGRectMake(0, self.tableView.bottom, self.width * 0.5, 49);
        self.buyButton.frame = CGRectMake(self.width * 0.5, self.tableView.bottom, self.width * 0.5, 49);
        [self setAddCartButtonAttributeTitle:self.currentPrice];
        [self setBuyButtonAttributeTitle:self.currentPGPrice];
    } else {
        self.buyButton.frame = CGRectMake(0, self.tableView.bottom, self.width, 49);
        [self.buyButton setTitle:@"确定" forState:UIControlStateNormal];
    }
}
- (void)setAddCartButtonAttributeTitle:(NSString *)price {
    [self.addCartButton.titleLabel setNumberOfLines:2];
    [self.addCartButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
    
    NSString *sourceStr = [NSString stringWithFormat:@"¥%@\n单独买",price];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:sourceStr];
    
    [str setAttributes:@{NSFontAttributeName:TextPingFangFontSize(11)} range:NSMakeRange(0, sourceStr.length)];
    [str setAttributes:@{NSFontAttributeName:TextPingFangFontSize(13)} range:[sourceStr rangeOfString:@"单独买"]];
    //    //行间距
    //    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //    paragraphStyle.lineSpacing = 0;
    //    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    //    [str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, sourceStr.length)];
    
    [self.addCartButton setAttributedTitle:str forState:UIControlStateNormal];
}
- (void)setBuyButtonAttributeTitle:(NSString *)price {
    [self.buyButton.titleLabel setNumberOfLines:2];
    [self.buyButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
    
    NSString *sourceStr = [NSString stringWithFormat:@"¥%@\n团购",price];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:sourceStr];
    
    [str setAttributes:@{NSFontAttributeName:TextPingFangFontSize(11)} range:NSMakeRange(0, sourceStr.length)];
    [str setAttributes:@{NSFontAttributeName:TextPingFangFontSize(13)} range:[sourceStr rangeOfString:@"团购"]];
    //    //行间距
    //    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //    paragraphStyle.lineSpacing = 0;
    //    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    //    [str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, sourceStr.length)];
    
    [self.buyButton setAttributedTitle:str forState:UIControlStateNormal];
}

- (void)setCurrentType:(GoodStandardViewType)currentType {
    _currentType = currentType;
    self.controlView.textField.text = @"1";
    if (currentType == GoodStandardViewTypeForAddCart) {
        self.addCartButton.hidden = NO;
        self.buyButton.hidden = YES;
        self.currentColor = [UIColor yellowButtonColor];
    } else if (currentType == GoodStandardViewTypeForBuy) {
        self.addCartButton.hidden = YES;
        self.buyButton.hidden = NO;
        self.currentColor = [UIColor orangePriceColor];
    } else if (currentType == GoodStandardViewTypeForAll || currentType == GoodStandardViewTypeGroupAll) {
        self.addCartButton.hidden = NO;
        self.buyButton.hidden = NO;
        self.currentColor = [UIColor orangePriceColor];
    } else if (currentType == GoodStandardViewTypeIntegral) {
        self.addCartButton.hidden = YES;
        self.buyButton.hidden = NO;
        self.currentColor = [UIColor orangePriceColor];
    } else if (currentType == GoodStandardViewTypeNearby) {
        self.controlView.textField.text = @"0";
        self.addCartButton.hidden = YES;
        self.buyButton.hidden = NO;
        self.currentColor = [UIColor orangePriceColor];
    } else {
        self.addCartButton.hidden = YES;
        self.buyButton.hidden = NO;
        self.currentColor = [UIColor orangePriceColor];
    }
}

#pragma mark - Method
- (void)removeSelf {
    [self removeObserver];
    [self removeAllSubviews];
    [self removeFromSuperview];
}

- (void)dismissSelf {
    NSMutableArray *responseArr = [NSMutableArray array];
    responseArr = [SKUManager getSelectAttrWithAllAttributeArr:self.dataArr];
    if (self.block) {
        NSInteger index = [self getAllAttributeForBlock];
        if (index != -1) {
            self.block(-1, responseArr, nil, self.controlView.textField.text);
        } else {
            self.block(-1, responseArr, self.responsePriceModel, self.controlView.textField.text);
        }
    }
    
    [self topToBottomAnimation];
}

- (void)bottomToTopAnimation {
    self.backView.frame = CGRectMake(0, self.height, self.width, ViewHeight);
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.backView.frame = CGRectMake(0, weakSelf.height - ViewHeight, weakSelf.width, ViewHeight);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)topToBottomAnimation {
    self.backView.frame = CGRectMake(0, self.height - ViewHeight, self.width, ViewHeight);
    
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.backView.frame = CGRectMake(0, weakSelf.height, weakSelf.width, ViewHeight);
    } completion:^(BOOL finished) {
        [weakSelf removeSelf];
    }];
}

- (void)configureFooter {
    self.bottomView.frame = CGRectMake(0, 0, self.width, 48);
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(15, 0, 100, self.bottomView.height);
    label.textColor = [UIColor blackTextColor];
    label.font = TextPingFangFontSize(16);
    
    label.text = @"数量";
    self.controlView.frame = CGRectMake(self.width - 15 - 32 * 3, 8, 32 * 3, 32);
    [self.bottomView addSubview:self.controlView];
    [self.bottomView addSubview:label];
    self.tableView.tableFooterView = self.bottomView;
}

- (void)reloadUI {
    [self reloadPrice];
    [self reloadSkuArr];
    [self.tableView reloadData];
}

- (void)reloadPrice {
    if ([SKUManager allAttributeSelect:self.dataArr]) {
        NSArray *arr = [SKUManager getFilterProduct:[SKUManager getSelectAttrWithAllAttributeArr:self.dataArr] withPriceSkuArr:self.goodsattributeArr];
        if (arr.count == 1) {
            self.responsePriceModel = arr.firstObject;
            self.controlView.maxCount = self.responsePriceModel.gastock.integerValue;
            if (self.controlView.maxCount > 0) {
                if (self.controlView.textField.text.integerValue > self.controlView.maxCount) {
                    self.controlView.textField.text = self.responsePriceModel.gastock;
                } else {
                    if (self.currentType != GoodStandardViewTypeNearby) {
                        if ([self.controlView.textField.text isEqualToString:@"0"]) {
                            self.controlView.textField.text = @"1";
                        }
                    } else {
                        self.controlView.textField.text = TextIsNotNil(self.responsePriceModel.selectNumber)?self.responsePriceModel.selectNumber:@"0";
                    }
                }
            } else {
                self.controlView.textField.text = @"0";
            }
            if (self.currentType == GoodStandardViewTypeIntegral) {
                self.priceLabel.text = [NSString stringWithFormat:@"%@积分", self.currentPrice];
            } else {
                self.priceLabel.text = [NSString stringWithFormat:@"¥%@", self.currentPrice];
            }
            //self.priceLabel.text = [NSString stringWithFormat:@"¥%@", self.responsePriceModel.gaprice];
            self.skuCountLabel.text = [NSString stringWithFormat:@"库存：%@", self.responsePriceModel.gastock];
        } else {
            [MBProgressHUD showError:[UIApplication sharedApplication].keyWindow title:@"没有库存" code:@"" animated:YES];
        }
    } else {
        if (self.currentType == GoodStandardViewTypeIntegral) {
            self.priceLabel.text = [NSString stringWithFormat:@"%@积分", self.currentPrice];
        } else {
            self.priceLabel.text = [NSString stringWithFormat:@"¥%@", self.currentPrice];
        }
        self.skuCountLabel.text = [NSString stringWithFormat:@"库存：0"];
        self.controlView.maxCount = 0;
    }
    
    // 计算宽度
    self.priceLabel.width = [self boundingWithStr:self.priceLabel.text];
    self.skuCountLabel.width = [self boundingWithStr:self.skuCountLabel.text];
    [self layoutSubviews];
}

#pragma mark - Action
- (void)buttonClick:(UIButton *)sender {
    // 判断规格是否都已选择
    NSInteger index = [self getAllAttributeForBlock];
    if (index != -1) {
        GoodAttributeModel *model = [self getGoodAttributeModel:index];
        NSString *message = @"请选择";
        message = [NSString stringWithFormat:@"%@%@",message, model.ganame];
        [MBProgressHUD myShowHUDAddedTo:[UIApplication sharedApplication].keyWindow labelText:message animated:YES];
        return;
    }
    // 判断是否没有库存或输入数量为0
    if ([self.controlView.textField.text isEqualToString:@"0"]) {
        if (self.responsePriceModel.gastock.integerValue <= 0) {
            [MBProgressHUD myShowHUDAddedTo:[UIApplication sharedApplication].keyWindow labelText:@"暂无库存" animated:YES];
            return;
        } else if (self.currentType != GoodStandardViewTypeNearby) {
            [MBProgressHUD myShowHUDAddedTo:[UIApplication sharedApplication].keyWindow labelText:@"请输入数量" animated:YES];
            return;
        }
    }
    
    // 修改已选择属性的数量
    if (self.responsePriceModel && self.currentType == GoodStandardViewTypeNearby) {
        if (TextIsNotNil(self.controlView.textField.text)) {
            self.responsePriceModel.selectNumber = self.controlView.textField.text;
        } else {
            self.responsePriceModel.selectNumber = @"0";
        }
    }
    
    // 移除自己
    [self dismissSelf];

    NSMutableArray *responseArr = [NSMutableArray array];
    responseArr = [SKUManager getSelectAttrWithAllAttributeArr:self.dataArr];
    if (self.block) {
        if (sender == self.addCartButton) {
            self.block(0, responseArr, self.responsePriceModel, self.controlView.textField.text);
        } else if (sender == self.buyButton) {
            self.block(1, responseArr, self.responsePriceModel, self.controlView.textField.text);
        }
    }
}

- (NSInteger)getAllAttributeForBlock {
    for (NSInteger i = 0; i < self.selectArr.count; i++) {
        NSNumber *number = [self.selectArr safeObjectAtIndex:i];
        if ([number isEqual:@(-1)]) {
            return i;
        }
    }
    return -1;
}

- (GoodAttributeModel *)getGoodAttributeModel:(NSInteger)index {
    GoodAttributeModel *model = [self.dataArr safeObjectAtIndex:index];
    return model;
}

- (void)resignTextField {
    [self.controlView.textField resignFirstResponder];
}

- (void)responseBlock {
    
    __block typeof(self) blockSelf = self;
    self.controlView.block = ^(NSString *text, BOOL isMax) {
        if (isMax) {
            [MBProgressHUD myShowHUDAddedTo:[UIApplication sharedApplication].keyWindow labelText:@"数量超出库存~" animated:YES];
        }
        if (self.currentType != GoodStandardViewTypeNearby && text.integerValue == 0 && self.controlView.maxCount > 0) {
            [blockSelf.controlView.textField setText:@"1"];
        }
    };
}

- (void)reloadSkuArr {
    //self.dataArr = [SKUManager setSelectState:self.dataArr value:[SKUManager getFilterAttrs:[SKUManager getSelectAttrWithAllAttributeArr:self.dataArr] withPriceSkuArr:self.goodsattributeArr andAttributeArr:self.dataArr]];
    self.dataArr = [SKUManager setSelectState:self.dataArr value:[SKUManager getCanSelectAttribute:[SKUManager getSelectAttrWithAllAttributeArr:self.dataArr] withPriceSkuArr:self.goodsattributeArr attrArr:self.dataArr]];
    
}

#pragma mark - Setters
// 创建dataArr时创建选择属性的数组
- (void)setDataArr:(NSMutableArray *)dataArr {
    _dataArr = dataArr;
    
    if (self.selectArr.count == 0) {
        for (id object in dataArr) {
            // 初始化值 -1
            [self.selectArr addObject:@(-1)];
        }
    }

}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GoodStandardSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    GoodAttributeModel *model = [self.dataArr safeObjectAtIndex:indexPath.section];
    [cell setTitlesArr:model.twoattribute withTitle:model.ganame withColor:self.currentColor];
    __weak typeof(self) weakSelf = self;
    cell.block = ^(NSInteger index) {
        // 取消返回-1 与初始化值也是 -1
        NSLog(@"select: index = %ld", (long)index);
        [weakSelf.selectArr replaceObjectAtIndex:indexPath.section withObject:@(index)];
//        [weakSelf reloadUI];
        
        GoodAttributeModel *model = [weakSelf.dataArr safeObjectAtIndex:indexPath.section];
        for (GoodAttributeValueModel *valueModel in model.twoattribute) {
            [valueModel setIsSelected:NO];
        }
        if (index != -1) {
            GoodAttributeValueModel *valueModel = [model.twoattribute safeObjectAtIndex:index];
            [valueModel setIsSelected:YES];
        }
        [weakSelf reloadUI];
    };
    return cell;
}


#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GoodAttributeModel *model = [self.dataArr safeObjectAtIndex:indexPath.section];
    NSArray *arr = [model getTitles];
    return [GoodStandardSelectCell getHeightWithArr:arr];
}

#pragma mark - Notification

#pragma mark - Getters
- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [[UIView alloc] init];
    }
    return _mainView;
}

- (UIView *)backView {
    if (!_backView) {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor whiteColor];
    }
    return _backView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView clearExtendCellLine];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
        _tableView.estimatedRowHeight = 44;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.size = CGSizeMake(60, 60);
        _imageView.backgroundColor = [UIColor imageEDBackgroundColor];
        [_imageView setContentMode:UIViewContentModeCenter];
    }
    return _imageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.textColor = [UIColor blackTextColor];
        _titleLabel.font = TextPingFangFontSize(16);
    }
    return _titleLabel;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.textColor = [UIColor orangePriceColor];
        _priceLabel.font = TextPingFangFontSize(14);
    }
    return _priceLabel;
}

- (UILabel *)skuCountLabel {
    if (!_skuCountLabel) {
        _skuCountLabel = [[UILabel alloc] init];
        _skuCountLabel.textColor = [UIColor grayTextColor];
        _skuCountLabel.font = TextPingFangFontSize(14);
    }
    return _skuCountLabel;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor borderColor];
    }
    return _lineView;
}

- (UIButton *)addCartButton {
    if (!_addCartButton) {
        _addCartButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addCartButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_addCartButton setBackgroundImage:[UIImage imageWithColor:[UIColor yellowButtonColor]] forState:UIControlStateNormal];
        [_addCartButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _addCartButton.titleLabel.font = TextPingFangFontSize(14);
        [_addCartButton setTitle:@"加入购物车" forState:UIControlStateNormal];
    }
    return _addCartButton;
}

- (UIButton *)buyButton {
    if (!_buyButton) {
        _buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buyButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_buyButton setBackgroundImage:[UIImage imageWithColor:[UIColor orangePriceColor]] forState:UIControlStateNormal];
        [_buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _buyButton.titleLabel.font = TextPingFangFontSize(14);
        [_buyButton setTitle:@"立即购买" forState:UIControlStateNormal];
    }
    return _buyButton;
}

- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
    }
    return _bottomView;
}

- (CartCountControlView *)controlView {
    if (!_controlView) {
        _controlView = [[CartCountControlView alloc] init];
        _controlView.textField.delegate = self;
        _controlView.maxCount = 0;
    }
    return _controlView;
}

- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [[UIView alloc] init];
        _maskView.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignTextField)];
        [_maskView addGestureRecognizer:tapGesture];
        _maskView.hidden = YES;
    }
    return _maskView;
}

#pragma mark - KeyBoardNotification
- (void)addObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)removeObserver {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    _keyboardAnimationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    _keyboardBenginFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    _keyboardEndFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    self.maskView.hidden = NO;
    [UIView beginAnimations:nil context:NULL];
    UIEdgeInsets insets = self.tableView.contentInset;
    insets.bottom = CGRectGetHeight(_keyboardEndFrame);
    [self.tableView setContentInset:insets];
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    _keyboardAnimationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    _keyboardBenginFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    _keyboardEndFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    self.maskView.hidden = YES;
    [UIView beginAnimations:nil context:NULL];
    UIEdgeInsets insets = self.tableView.contentInset;
    insets.bottom = 0;
    [self.tableView setContentInset:insets];
    [UIView commitAnimations];
    
    if ([self.controlView.textField.text isEqualToString:@"0"] && self.currentType != GoodStandardViewTypeNearby) {
        [self.controlView.textField setText:@"1"];
    }
}

@end
